<?php
	function addBorder($name)
	{
		$insert  = "INSERT INTO ".DB_PERFIX."com_borders (`id`, `name`)"; 
		$insert .= "VALUES (NULL, '".$_POST['name']."');";
		$result = mysql_query($insert);
		if ($result > 0)
		{
			$result = mysql_query("SELECT id FROM ".DB_PERFIX."com_borders WHERE name LIKE '".$_POST['name']."';");
			while($row = mysql_fetch_array($result))
			{
				$item['id'] = $row['id'];
				$item['title'] = $name;
			}
		}
		return $item;
	}
	
	function getBorders($incat=0)
	{
		$i=0;
		if($incat > 0)
		{ $result = mysql_query("SELECT * FROM ".DB_PERFIX."com_borders WHERE id IN(SELECT item_id FROM ".DB_PERFIX."admin_hfb_cats_to_items WHERE type=6 AND cat_id=".$incat.");"); }
		else
		{ $result = mysql_query("SELECT * FROM ".DB_PERFIX."com_borders;"); }
		while($row = mysql_fetch_array($result))
		{
			$border[$i]['id'] = $row['id'];
			$border[$i]['type'] = $row['type'];
			$border[$i]['name'] = $row['name'];
			$border[$i]['width'] = $row['width'];
			$border[$i]['image_bg'] = $row['image_bg'];
			$border[$i]['image_top'] = $row['image_top'];
			$border[$i]['image_top_l'] = $row['image_top_l'];
			$border[$i]['image_top_r'] = $row['image_top_r'];
			$border[$i]['image_bot'] = $row['image_bot'];
			$border[$i]['image_bot_l'] = $row['image_bot_l'];
			$border[$i]['image_bot_r'] = $row['image_bot_r'];
			$border[$i]['image_mid_l'] = $row['image_mid_l'];
			$border[$i]['image_mid_r'] = $row['image_mid_r'];
			$border[$i]['active'] = $row['active'];
			$i++;
		}
		return $border;
	}
	function updateBorder($id, $name, $type, $width)
	{
		$update  = 'UPDATE '.DB_PERFIX.'com_borders SET ';
		$update .= 'name="'.$name.'", ';
		$update .= 'type='.$type.', ';
		$update .= 'width='.$width.' ';
		$update .= 'WHERE id='.$id.' LIMIT 1;';
		$update = mysql_query($update);
	}
	function deleteBorder($id)
	{
		if($id > 0)
		{
			$item = getBorderAdmin($id);
			if($item)
			{
				if($item['image_bg'] != '')
				{ deleteBorderImage($id, 'image_bg', $item['image_bg']); }
				
				if($item['image_top'] != '')
				{ deleteBorderImage($id, 'image_top', $item['image_top']); }
				if($item['image_top_l'] != '')
				{ deleteBorderImage($id, 'image_top_l', $item['image_top_l']); }
				if($item['image_top_r'] != '')
				{ deleteBorderImage($id, 'image_top_r', $item['image_top_r']); }

				if($item['image_bot'] != '')
				{ deleteBorderImage($id, 'image_bot', $item['image_bot']); }
				if($item['image_bot_l'] != '')
				{ deleteBorderImage($id, 'image_bot_l', $item['image_bot_l']); }
				if($item['image_bot_r'] != '')
				{ deleteBorderImage($id, 'image_bot_r', $item['image_bot_r']); }
				
				if($item['image_mid_l'] != '')
				{ deleteBorderImage($id, 'image_mid_l', $item['image_mid_l']); }
				if($item['image_mid_r'] != '')
				{ deleteBorderImage($id, 'image_mid_r', $item['image_mid_r']); }
				
				$result = mysql_query('DELETE FROM '.DB_PERFIX.'com_borders WHERE id='.$id.';');
				return $result;
				
				reloadHFBTypeCss(6);
			}
		}
	}
	function deleteBorderImage($id, $field, $filename)
	{
		if (!unlink(BORDERS_PATH.$filename))
		{ $status = "Error deleting ".BORDERS_PATH.$filename; }
		else
		{ 
			$delete = 'UPDATE '.DB_PERFIX.'com_borders SET '.$field.' = "'.'" WHERE id='.$id.';';
			$result = mysql_query($delete);
			
			if ($result > 0)
			{$status = "Image Deleted";}
		}
		return $status;
	}
	function uploadBorderImages($id)
	{	
		$error['image_bg'] = uploadBorderImage($id, 'image_bg', '_bg.');
		$error['image_top'] = uploadBorderImage($id, 'image_top', '_t_bg.');
		$error['image_top_l'] = uploadBorderImage($id, 'image_top_l', '_t_l.');
		$error['image_top_r'] = uploadBorderImage($id, 'image_top_r', '_t_r.');
		
		$error['image_bot'] = uploadBorderImage($id, 'image_bot', '_b_bg.');
		$error['image_bot_l'] = uploadBorderImage($id, 'image_bot_l', '_b_l.');
		$error['image_bot_r'] = uploadBorderImage($id, 'image_bot_r', '_b_r.');
		
		$error['image_mid_l'] = uploadBorderImage($id, 'image_mid_l', '_l_bg.');
		$error['image_mid_r'] = uploadBorderImage($id, 'image_mid_r', '_r_bg.');
		return $error;
	}
	
	function uploadBorderImage($id, $field, $subtitle)
	{
		if (image_valid($_FILES[$field]['type']) && ($_FILES[$field]["size"] < 20000) && ($_FILES[$field]['name'] != ""))
		{
			$temp_name = $_FILES[$field]["name"];
			$extension = substr($temp_name,-3);
			$new_name = "type_".$id.$subtitle.$extension;
			
			if (file_exists(BORDERS_PATH.$new_name))
			{$error = $_FILES[$field]["name"] . " already exists. ";}
			else
			{
				
				move_uploaded_file($_FILES[$field]["tmp_name"], BORDERS_PATH.$new_name);
				$error = "Stored in: " . BORDERS_PATH.$new_name;
				
				$result = mysql_query("UPDATE ".DB_PERFIX."com_borders SET ".$field." = '".$new_name."' WHERE id = '".$id."'");
				if ($result > 0)
				{$error = "Image Uploaded ".$new_name;}
			}
		}
		return $error;
	}
?>